package com.example.community.userHome;

import com.example.community.dao.ComplaintDao;
import com.example.community.model.Complaint;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;

@WebServlet(urlPatterns = "/updateComplaintServlet")
public class UpdateComplaintServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cpNo = req.getParameter("cpNo");
        req.setAttribute("cpNo",cpNo);
        req.getRequestDispatcher("/WEB-INF/userHome/UpdateComplaint.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Integer currentUserUsNo = (Integer) req.getSession().getAttribute("currentUserUsNo");
            String cpNo = req.getParameter("cpNo");
            String desc = req.getParameter("desc");

            Calendar calendar = Calendar.getInstance();

            ComplaintDao complaintDao = new ComplaintDao();

            Complaint complaint = complaintDao.fandById(Integer.parseInt(cpNo));

//            Complaint complaint = new Complaint();
//            complaint.setCpNo(Integer.parseInt(cpNo));
            complaint.setCpContent(desc);
//            complaint.setUsNo(currentUserUsNo);
//            complaint.setCpDt(calendar.getTime());
//            complaint.setCpState("未处理");


            Integer update = complaintDao.update(complaint);
            if (update == 1){
                resp.getWriter().write("true");
            }else {
                resp.getWriter().write("false");
            }
        }catch (Exception e){
            resp.getWriter().write("false");
        }
    }
}
